package com.zxjbyte.yiyi.server.gui;

import javax.swing.*;
import java.awt.*;
import java.nio.file.Paths;
import java.util.Calendar;

/**
 * 时间gui
 *
 * @Author zhangxingjia
 * @Date 2023/7/18 18:03
 * @Version: 1.0
 */
public class Clock extends JComponent {
    private static final long serialVersionUID = -5767474270407608805L;

    private final Font f2 = new Font("宋体", Font.BOLD, 20);
    private final JLabel display = new JLabel();
    private final JLabel display2 = new JLabel("");
    private int hour;
    private int min;
    private int sec;
    private final Calendar calendar;
    private final double PI = Math.PI;

    public Clock() {
        JLabel l = new JLabel("当前时间：");
        add(l);
        l.setBounds(120, 320, 80, 20);
        Font f = new Font("微软雅黑", Font.PLAIN, 15);
        l.setFont(f);
        add(display);
        display.setBounds(195, 320, 80, 20);
        display.setFont(f);
        display.setBorder(BorderFactory.createLineBorder(Color.black));
        add(display2);
        display2.setBounds(90, 350, 250, 20);
        display2.setFont(f);
        calendar = Calendar.getInstance();
        hour = calendar.get(Calendar.HOUR_OF_DAY);
        min = calendar.get(Calendar.MINUTE);
        sec = calendar.get(Calendar.SECOND);
        setVisible(true);
    }

    public void paintComponent(Graphics g1) {
        double x, y;
        super.paintComponent(g1);
        Graphics2D g2d = (Graphics2D) g1;
        //反锯齿开关开
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        // 作者
        g2d.setFont(new Font("Times New Roman", Font.BOLD, 14));
        g2d.setColor(Color.BLUE);
        g2d.drawString("Produced by Zhang XingJia", 110, 22);

        //画表盘
//        GradientPaint gradient = new GradientPaint(5, 40, Color.red, 15, 50, Color.YELLOW, true);
        GradientPaint gradient = new GradientPaint(75, 40, Color.RED, 300, 250, Color.GREEN, true);
        // 设置渐变颜色
        g2d.setPaint(gradient);
        g2d.setStroke(new BasicStroke(3, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
//        g.drawOval(75, 40, 250, 250);
        g2d.drawOval(73, 39, 250, 250);
        g2d.fillOval(195, 160, 10, 10);
        g2d.setColor(Color.black);

        //画60个点
        for (int i = 0; i < 60; i++) {
            double[] co = paint_Dot(i * 2 * PI / 60);
            x = co[0];
            y = co[1];
            if (i % 5 == 0) {//画12个大点
                g2d.fillOval((int) (x - 5 + 200), (int) (y - 5 + 165), 8, 8);
            } else { //其他小点
                g2d.fillOval((int) (x - 2.5 + 200), (int) (y - 2.5 + 165), 3, 3);
            }
        }

        //画四个数字
        g2d.setFont(f2);
        g2d.drawString("3", 296, 172);
        g2d.drawString("6", 193, 271);
        g2d.drawString("9", 91, 171);
        g2d.drawString("12", 188, 71);  //g.drawString("12", 190, 68);

        //画时针，分针，秒针
        paint_HourPointer(hour * 3600 + min * 60 + sec, g2d);//时针走过的秒数
        paint_MinutePointer(min * 60 + sec, g2d);//分针走过的秒数
        paint_SecondPointer(sec, g2d);//秒针走过的秒数
    }



    public void showUI(){
        // 使用 javax.swing.Timer，确保在 Swing 线程中执行
        Timer timer = new Timer(1000, e -> {
            hour = calendar.get(Calendar.HOUR_OF_DAY);
            min = calendar.get(Calendar.MINUTE);
            sec = calendar.get(Calendar.SECOND);
            showTime();
            repaint();
            calendar.add(Calendar.SECOND, 1);
        });
        timer.start();
    }

    public void paint_HourPointer(int second, Graphics2D g) {//second表示当前时间的时针相对00:00:00走了多少秒
        double x, y, angle;
        angle = second * PI / 21600;//时针的速度为PI/21600 (rad/s)
        x = 200 + 60 * Math.sin(angle);
        y = 165 - 60 * Math.cos(angle);
        g.setStroke(new BasicStroke(5, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND));
        g.setPaint(new GradientPaint(200, 165, Color.red, 260, 165, Color.blue, true));
        g.drawLine(200, 165, (int) x, (int) y);
    }

    public void paint_MinutePointer(int second, Graphics2D g) {//second表示当前时间的分针相对00:00:00走了多少秒
        double x, y, angle;
        angle = second * PI / 1800;//分针的速度为PI/1800 (rad/s)
        x = 200 + 80 * Math.sin(angle);
        y = 165 - 80 * Math.cos(angle);
        g.setStroke(new BasicStroke(3, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND));
        g.setPaint(new GradientPaint(200, 165, Color.magenta, 280, 165, Color.blue, true));
        g.drawLine(200, 165, (int) x, (int) y);
    }

    public void paint_SecondPointer(int second, Graphics2D g) {//second表示当前时间的秒针相对00:00:00走了多少秒
        double x, y, x1, y1, x2, y2, x3, y3, angle;
        double cos = 90 / Math.sqrt(8125);//90*90+5*5
        double sin = 5 / Math.sqrt(8125);
        angle = second * PI / 30;//时针的速度为PI/30 (rad/s)
        x = 200 + 95 * Math.sin(angle);
        y = 165 - 95 * Math.cos(angle);
        x1 = 200 + 20 * Math.sin(angle + PI);
        y1 = 165 - 20 * Math.cos(angle + PI);
        x2 = 200 + Math.sqrt(8125) * (Math.sin(angle) * cos - Math.cos(angle) * sin); //sin(a-b)
        y2 = 165 - Math.sqrt(8125) * (Math.cos(angle) * cos + Math.sin(angle) * sin); //cos(a-b)
        x3 = 200 + Math.sqrt(8125) * (Math.sin(angle) * cos + Math.cos(angle) * sin); //sin(a+b)
        y3 = 165 - Math.sqrt(8125) * (Math.cos(angle) * cos - Math.sin(angle) * sin); //cos(a+b)
        g.setStroke(new BasicStroke(2, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
        g.setPaint(new GradientPaint(180, 165, Color.CYAN, 295, 165, Color.MAGENTA, true));
        g.drawLine((int) x1, (int) y1, (int) x, (int) y);
        g.drawLine((int) x2, (int) y2, (int) x, (int) y);
        g.drawLine((int) x3, (int) y3, (int) x, (int) y);
    }

    public double[] paint_Dot(double angle) {
        double[] co = new double[2];
        co[0] = 115 * Math.cos(angle);//横坐标
        co[1] = 115 * Math.sin(angle);//纵坐标
        return co;
    }

    private void showTime() {
        String date;
        String strTime;
        if (hour < 10) {
            strTime = "0" + hour + ":";
        } else {
            strTime = "" + hour + ":";
        }

        if (min < 10) {
            strTime = strTime + "0" + min + ":";
        } else {
            strTime = strTime + "" + min + ":";
        }

        if (sec < 10) {
            strTime = strTime + "0" + sec;
        } else {
            strTime = strTime + "" + sec;
        }
        //在窗体上设置显示时间
        date = " " + (calendar.get(Calendar.YEAR)) + "年" + (calendar.get(Calendar.MONTH) + 1) + "月" + calendar.get(Calendar.DAY_OF_MONTH) + "日   " + "星期";
        switch (calendar.get(Calendar.DAY_OF_WEEK)) {
            case Calendar.MONDAY:
                date += "一";
                break;
            case Calendar.TUESDAY:
                date += "二";
                break;
            case Calendar.WEDNESDAY:
                date += "三";
                break;
            case Calendar.THURSDAY:
                date += "四";
                break;
            case Calendar.FRIDAY:
                date += "五";
                break;
            case Calendar.SATURDAY:
                date += "六";
                break;
            case Calendar.SUNDAY:
                date += "日";
                break;
        }
        date += "  CST";  //Chinese standard time
        strTime = "  " + strTime;

        display.setText(strTime);
        display2.setText(date);
    }

    public static void main(String[] args) {
        Clock c = new Clock();
        c.showUI();
        JFrame f = new JFrame("北京时间");
        Image img = Toolkit.getDefaultToolkit().getImage(Paths.get("").toAbsolutePath() + "/static/icon/clock-icon.png"); //窗口图标
        f.setIconImage(img);
        f.setSize(420, 420);
        f.setResizable(false);
        f.add(c, BorderLayout.CENTER);
        f.setLocationRelativeTo(null);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }
}
